package com.compass.doctor.ui.home

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import com.compass.doctor.databinding.ActivitySpecialColumnBinding
import com.compass.doctor.ui.home.fragment.ArticleFragment
import com.compass.doctor.ui.home.fragment.VideoFragment
import com.compass.framework.adapter.ViewPage2FragmentAdapter
import com.compass.doctor.base.BaseVBActivity
import com.compass.framework.constant.KEY_CONSULT_TYPE
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.constant.KEY_PATIENT_ID

/**
 * Description： 专栏内容
 * Date:2023/12/26 14:15
 */
class SpecialColumnActivity : BaseVBActivity<ActivitySpecialColumnBinding>(){
    companion object {
        fun start(context: Context,comeType:Int,patientId:String?=null) {
            val intent = Intent(context, SpecialColumnActivity::class.java)
            intent.putExtra(KEY_CONSULT_TYPE,comeType)
            intent.putExtra(KEY_PATIENT_ID,patientId)
            context.startActivity(intent)
        }
    }

    private lateinit var viewPageAdapter: ViewPage2FragmentAdapter
    private var fragments: ArrayList<Fragment> = ArrayList()
    private lateinit var articleFragment: ArticleFragment//文章
    private lateinit var videoFragment: VideoFragment//视频
    private var patientId:String?=null
    private var comeType:Int=1
    private var toIndex:Int=0
    override fun initView(savedInstanceState: Bundle?) {
        patientId=intent.getStringExtra(KEY_PATIENT_ID)
        comeType=intent.getIntExtra(KEY_CONSULT_TYPE,1)
        initFragment()
        mBinding.dslTab.tabIndicator.indicatorWidth = -2
        mBinding.dslTab.tabIndicator.indicatorEnableFlow = true
        mBinding.dslTab.configTabLayoutConfig {
            onSelectIndexChange = { _, selectIndexList, _, _ ->
                toIndex = selectIndexList.first()
                mBinding.viewpager2.setCurrentItem(toIndex, false)
            }
        }
        viewPageAdapter = ViewPage2FragmentAdapter(supportFragmentManager, lifecycle, fragments)
        mBinding.viewpager2.adapter = viewPageAdapter
        mBinding.viewpager2.isUserInputEnabled = false//禁止viewPage滑动
        mBinding.viewpager2.setCurrentItem(0, false)
    }


    override fun initData() {
        mBinding.edtDiagnosis.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                goToSearch()
            }
            false
        }
        mBinding.edtDiagnosis.addTextChangedListener {
            if (it.toString().isEmpty()) {
                goToSearch()
            }
        }
    }

    private fun  goToSearch(){
       var key = mBinding.edtDiagnosis.text.toString().trim()
        if (toIndex==0){
            articleFragment.searchArticle(key)
        }else{
            videoFragment.searchVideo(key)
        }


    }
    private fun initFragment() {
        articleFragment = ArticleFragment()
        val bundleAr = Bundle()
        bundleAr.putInt(KEY_ORDER_TYPE, comeType)
        bundleAr.putString(KEY_PATIENT_ID, patientId)
        articleFragment.arguments = bundleAr
        fragments.add(articleFragment)

        videoFragment = VideoFragment()
        val bundleVi = Bundle()
        bundleVi.putInt(KEY_ORDER_TYPE, comeType)
        bundleVi.putString(KEY_PATIENT_ID, patientId)
        videoFragment.arguments = bundleVi
        fragments.add(videoFragment)

    }
}